import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    redirect: '/login'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/home/about',
    children: [
      {
        path: 'about',
        name: 'about',
        component: () => import('../views/AboutView.vue')
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/userinfo/UserinfoList.vue')
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('../views/category/CategoryList.vue')
      }, {
        path: 'banner',
        name: 'banner',
        component: () => import('../views/banner/BannerList.vue')
      }, {
        path: 'director',
        name: 'director',
        component: () => import('../views/director/DirectorList.vue')
      }, {
        path: 'movie',
        name: 'movie',
        component: () => import('../views/movie/MovieList.vue')
      }
    ]
  }, {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
/* 路由前置守卫 */
router.beforeEach((to, from, next) => {
  if (to.path.match('home')) {
    if (Vue.$cookies.get('token')) {
      next()
    } else {
      Vue.prototype.$alert('请先登录！', { type: 'error' }).then(() => {
        next({ path: '/login' })
      })
    }
  } else {
    next()
  }
})

export default router
